package com.service.business.borrow.controller;

import com.service.business.borrow.controller.rep.MemberBorrowDetailsRep;
import com.service.business.borrow.service.MemberBorrowService;
import com.service.business.borrow.controller.req.MemberBorrowAddReq;
import com.service.business.borrow.controller.req.MemberBorrowDetailsReq;
import com.service.business.borrow.controller.req.PageMemberBorrowReq;
import com.service.business.framework.log.RequestLog;
import com.service.business.support.SessionUtil;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.enums.CoinsEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/borrow")
@Api(tags = "借贷")
public class BorrowController extends BaseController {

    @Autowired
    private MemberBorrowService memberBorrowService;
    @Autowired
    private SessionUtil sessionUtil;


    @ApiOperation("加载借贷数据和利息数据")
    @RequestLog("加载借贷数据和利息数据")
    @GetMapping("/config")
    public BaseResponse initConfig(PageMemberBorrowReq pageMemberBorrowReq){
        return successQueryOneResponse(memberBorrowService.getInitConfig(pageMemberBorrowReq.getCoinsId()));
    }


    @ApiOperation("用户开通借贷")
    @RequestLog("用户开通借贷-请求")
    @PostMapping("/apply")
    public BaseResponse saveMemberBorrow(@RequestBody MemberBorrowAddReq memberBorrowAddReq){
        memberBorrowService.addBorrow(memberBorrowAddReq);
        return successSaveBaseResponse();
    }

    @ApiOperation("分页查询用户借贷列表")
    @RequestLog("分页查询用户借贷列表")
    @PostMapping("/member-page/{coinsName}")
    public PageResponse<MemberBorrowDetailsRep> pageMemberBorrow(@PathVariable("coinsName") CoinsEnum coins,
                                                                 @ApiParam @RequestBody PageMemberBorrowReq pageMemberBorrowReq){
        return successQueryPageResponse(memberBorrowService.listMemberBorrowByParams(coins,pageMemberBorrowReq));
    }

    @ApiOperation("用户借贷详情")
    @RequestLog("用户借贷详情")
    @PostMapping("/details")
    public BaseResponse detailsBorrow(@ApiParam @RequestBody MemberBorrowDetailsReq request){
        return successQueryOneResponse(memberBorrowService.details(request.getBorrowId()));
    }
}
